package com.zhangst.bbs.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zhangst.bbs.domain.Cate;
import com.zhangst.bbs.domain.Content;
import com.zhangst.bbs.domain.User;
import com.zhangst.bbs.page.ContentModel;
import com.zhangst.bbs.page.DataList;
import com.zhangst.bbs.service.ContentServiceI;


@ParentPackage("basePackage")
@Namespace("/")
public class ContentAction implements ModelDriven<ContentModel>{

	private ContentServiceI contentService;
	private ContentModel contentModel = new ContentModel();
	public ContentServiceI getContentService() {
		return contentService;
	}
	
	@Autowired
	public void setContentService(ContentServiceI contentService) {
		this.contentService = contentService;
	}
	
	@Action(value="post")
	public String post() throws IOException{
		Content content = new Content();
		Cate cate = new Cate();
		cate.setId(contentModel.getCate());
		content.setCate(cate);
		content.setTitle(contentModel.getTitle());
		content.setContent_data(contentModel.getContent());
		content.setCreateDate(new Timestamp(new Date().getTime()));
		contentService.add(content);
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		User user = (User)session.get("user");
		if(user!=null){
		content.setUser(user);
		contentService.add(content);
		}
		ServletActionContext.getResponse().sendRedirect("content.action?cate="+cate.getId());
		return null;
	}
	
	@Action(value="content",results = { @Result(name = "success", location = "/WEB-INF/page/content.jsp") })
	public String content(){
		int cate = contentModel.getCate();
		int page = contentModel.getPage();
		int rows = contentModel.getRows();
		if(page<=0){
			page=1;
		}
		if(rows<=0){
			rows=10;
		}
		DataList data = contentService.getContents(cate, page, rows);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("data", data);
		return "success";
	}
	
	
	@Action(value="delContent")
	public void del() throws IOException{
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		User user = (User)session.get("user");
		if(user!=null&&user.getRole().getId()==2){
		Content content = new Content();
		content.setId(contentModel.getId());
		contentService.del(content);
		}
		ServletActionContext.getResponse().sendRedirect("content.action?cate="+contentModel.getCate());
		
	}
	@Override
	public ContentModel getModel() {
		return contentModel;
	}
	
	
	
}
